﻿using FarPoint.Web.Spread;
using System;
using System.Data;
using System.Data.OleDb;

namespace ControlExplorer.samples.LoadOnDemand
{
  public partial class TimedLoadOnDemand : SpreadDemoPage
  {
    protected void Page_Load(object sender, EventArgs e)
    {
      string js = "var spid='" + FpSpread1.ClientID + "';";
      js += "var ss=document.getElementById(spid);" + "\r\n";
      js += "if (ss.addEventListener) {" + "\r\n";
      js += "    ss.addEventListener(\"LoadRowsStart\", RowStart, false);" + "\r\n";
      js += "    ss.addEventListener(\"LoadRowsStopped\", RowStop, false);" + "\r\n";
      js += "} else {" + "\r\n";
      js += "    ss.onLoadRowsStart = RowStart;" + "\r\n";
      js += "    ss.onLoadRowsStopped = RowStop;" + "\r\n";
      js += "}" + "\r\n";

      ClientScript.RegisterStartupScript(this.GetType(), "onStartUpScript", js, true);

      if (IsPostBack)
      {
        return;
      }

      InitSpread(FpSpread1);

      InitSpreadStyles(FpSpread1.Sheets[0]);
      this.FpSpread1.UseClipboard = false;
    }

    private void InitSpread(FarPoint.Web.Spread.FpSpread spread)
    {
      FpSpread1.DataSource = GetOrders();
      spread.CssClass = "spreadStyle";
      spread.UseClipboard = false;
    }

    private void InitSpreadStyles(FarPoint.Web.Spread.SheetView sheet)
    {
      sheet.ColumnCount = 8;

      sheet.Columns[0].Width = 60;
      sheet.Columns[1].Width = 200;
      sheet.Columns[2].Width = 150;
      sheet.Columns[3].Width = 50;
      sheet.Columns[4].Width = 100;
      sheet.Columns[5].Width = 100;
      sheet.Columns[6].Width = 100;
      sheet.Columns[7].Width = 200;

      DateTimeCellType dt = new DateTimeCellType();
      dt.FormatString = "MM/dd/yyyy";
      sheet.Columns[4].CellType = dt;
      sheet.Columns[5].CellType = dt;

      sheet.AllowLoadOnDemand = true;
      sheet.PageSize = 100;
      sheet.LoadInitRowCount = 10;
      sheet.LoadRowIncrement = 5;
      sheet.LoadOnDemandMode = FarPoint.Web.Spread.LoadOnDemandMode.Background;
      sheet.LoadOnDemandTriggerMode = FarPoint.Web.Spread.LoadOnDemandTriggerMode.Timed;
      sheet.LoadOnDemandInterval = 1000;
    }

    DataSet GetOrders()
    {
      string constr = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=|DataDirectory|\Northwind.mdb;Persist Security Info=True";

      string sqlstr =
@"SELECT TOP 100 
        Orders.OrderID, 
        Customers.CompanyName as Customer, 
        Employees.Title as Employee, 
        SUM(details.Quantity) as Quantity, 
        Orders.RequiredDate, 
        Orders.ShippedDate, 
        Orders.ShipCity, 
        Orders.ShipAddress 
  FROM ((Orders INNER JOIN Customers ON Orders.CustomerID = Customers.CustomerID) 
        INNER JOIN Employees ON Orders.EmployeeID = Employees.EmployeeID) 
  INNER JOIN [Order Details] as details ON Orders.OrderID = details.OrderID 
  GROUP BY  Orders.OrderID, 
            Customers.CompanyName, 
            Employees.Title, 
            Orders.RequiredDate, 
            Orders.ShippedDate, 
            Orders.ShipCity, 
            Orders.ShipAddress";

      DataSet orders = new DataSet();
      using (OleDbConnection myconn = new OleDbConnection(constr))
      {
        OleDbDataAdapter sqlAdapter1 = new OleDbDataAdapter(sqlstr, myconn);
        sqlAdapter1.Fill(orders, "Orders");
        orders.Tables[0].Columns[0].Caption = "订单号";
        orders.Tables[0].Columns[1].Caption = "客户ID";
        orders.Tables[0].Columns[2].Caption = "雇员ID";
        orders.Tables[0].Columns[3].Caption = "订单日期";
        orders.Tables[0].Columns[4].Caption = "商机日期";
        orders.Tables[0].Columns[5].Caption = "下单日期";
        orders.Tables[0].Columns[6].Caption = "装货经由";
        orders.Tables[0].Columns[7].Caption = "运费";
        //orders.Tables[0].Columns[8].Caption = "船名";
        //orders.Tables[0].Columns[9].Caption = "送货地址";
        //orders.Tables[0].Columns[10].Caption = "送货城市";
        //orders.Tables[0].Columns[11].Caption = "送货区域";
        //orders.Tables[0].Columns[12].Caption = "送货邮编";
        //orders.Tables[0].Columns[13].Caption = "送货国家";
      }

      return orders;
    }
  }
}